Welcome to python!

4.10 循环结构练习(2)

案例3:输入任意学员的分数,获取最高分,最低分,平均分

maxScore=0

minScore=0

count=0

total=0

while True:

count=count+1

scores=int(input("请输入第"+str(count)+"个分数:"))

if count==1:

maxScore=scores

minScore=scores

if scores>maxScore:

maxScore=scores

if scores<maxScore:

maxScore=scores

total=total+scores

choice=input("结束请输入1,继续请输入其他值:")

if choice=="1":

break

print("最高分",maxScore,"最低分",minScore,"平均分",total/count)

返回值:

请输入第1个分数:45

结束请输入1,继续请输入其他值:2

请输入第2个分数:7888

结束请输入1,继续请输入其他值:2

请输入第3个分数:456

结束请输入1,继续请输入其他值:1

最高分 456 最低分 45 平均分 2796.3333333333335


案例4:修改餐厅结账程序,实现重复添加多个菜品

totalAmount=0

tatalCount=0

while True:

num1="1001"

price1=35

name1="爆炒猪肚"

num2="1002"

price2=15

name2="客家豆腐"

num3="1003"

price3=30

name3="红烧排骨"

#提前声明变量保存价格和名称

price=0

name=""

num=str(input("请输入菜品编号:"))

count=int(input("请输入购买数量:"))

if num==num1:

price=price1

name=name1

elif num==num2:

price=price2

name=name2

elif num==num3:

price=price3

name=name3

else:

print("你输入的菜品不存在,请确认")

amount=price*count

totalAmount=totalAmount+ amount

tatalCount=tatalCount+count

print("你当前添加的是:",name,",单价",price,"元,数量",count,"份,金额",amount)

choice=input("结束请输入1,继续请输入其他值:")

if choice=="1":

break

print("您总共消费了",totalAmount,"元!菜品数量:",tatalCount)

返回值:

请输入菜品编号:1001

请输入购买数量:2

你当前添加的是: 爆炒猪肚 ,单价 35 元,数量 2 份,金额 70

结束请输入1,继续请输入其他值:2

请输入菜品编号:1002

请输入购买数量:3

你当前添加的是: 客家豆腐 ,单价 15 元,数量 3 份,金额 45

结束请输入1,继续请输入其他值:2

请输入菜品编号:1003

请输入购买数量:5

你当前添加的是: 红烧排骨 ,单价 30 元,数量 5 份,金额 150

结束请输入1,继续请输入其他值:1

您总共消费了 265 元!菜品数量: 10